
import VueRouter from 'vue-router';
import Vue from 'vue';
Vue.use(VueRouter)

// 导入对应的页面组件
// import Home from '@/views/Home.vue'
// import Topic from '@/views/Topic.vue'
// import Category from '@/views/Category.vue'
// import Cart from '@/views/Cart.vue'
// import Mine from '@/views/Mine.vue'
// import Adress from '@/views/Adress'

// import searchPop from '@/views/searchpop.vue'
// import List from '@/views/List.vue'
// import Detail from '@/views/Detail.vue'


// / 该段代码不需要记，理解即可
// h 在vue-router在3.1.0版本之后，push和replace方法会返回一个promise对象，如果跳转到相同的路由，
// 就报promise uncaught异常
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};
// import searchPop from '@/views/searchPop.vue'
// 第一步：创建路由配置
const routes = [
    { path: '', redirect: '/home' }, // 路由重定向
    {
        path: '/home',
        component: ()=>import('@/views/Home.vue'),
        name: 'home',
        children: [
            {
                path: 'searchpop',
                component: ()=>import('@/views/searchpop.vue'),
                name: 'searchpop',
                meta: {
                    showtarbar: false
                }
            }
        ],
        meta: {
            showtarbar: true
        }
    },
    {
        path: '/topic',
        component: ()=>import('@/views/Topic.vue'),
        name: 'topic',
        meta: {
            showtarbar: true
        }
    },
    {
        path: '/category',
        component: ()=>import('@/views/Category.vue'),
        name: 'category',
        meta: {
            showtarbar: true
        }
    },
    {
        path: '/cart',
        component: ()=>import('@/views/Cart.vue'),
        name: 'cart',
        meta: {
            showtarbar: true
        }
    },
    {
        path: '/mine',
        component: ()=>import('@/views/Mine.vue'),
        name: 'mine',
        meta: {
            showtarbar: true
        }
    },
    {
        path:'/adress',
        component:()=>import('@/views/Adress'),
        name:'adress',


    },
    {
        path: '/list',
        component: ()=>import('@/views/List.vue'),
        name: 'list',
        meta: {
        showtarbar: false
        }
    },
    {
        path: '/detail',
        component: ()=>import('@/views/Detail.vue'),
        name: 'detail',
        meta: {
            showtarbar: false
        }
    },
]

// 第二步：创建路由实例对象
const router = new VueRouter({
    mode: 'history',
    routes
})


//添加路由守卫(全局)

router.beforeEach((to,from,next)=>{
    if(to.path=='/cart'){
        //判断是否登录
        let user=localStorage.getItem('user')
        if(user){
            next()
        }else{
            Vue.prototype.$toast.fail('请先去登陆吧')
            setTimeout(()=>{
                next('/mine')
            },
            1000)
        }

    }else{
        next()
    }
})
export default router



